/*
 * @Author: hcq
 * @version: 1.0.0
 * @Date: 2023-04-07
 */
import CallApp from "callapp-lib";

//唤起客户端相关参数
const iosScheme = {
  protocol: "ChinaBlueTV",
  host: "CallingAppFlag",
};
const androidScheme = {
  protocol: "chinablue",
  host: "cztvrouter",
};
const universalHost: string =
  window.location.host == "zmtv.cztv.com"
    ? "mhudong.cztv.com"
    : "zmtv.cztv.com";
const userAgent = window.navigator.userAgent || "";
const options = {
  scheme: /iphone|ipad|ipod/i.test(userAgent) ? iosScheme : androidScheme,
  intent: {
    package: "com.chinablue.tv",
    scheme: "chinablue",
  },
  universal: {
    host: universalHost,
  },
  appstore: "https://itunes.apple.com/cn/app/zhong-guo-lantv/id988616225?mt=8",
  yingyongbao: "https://a.app.qq.com/o/simple.jsp?pkgname=com.chinablue.tv",
  fallback: "https://zmtv.cztv.com/ap/download/index.shtml",
  timeout: 2000,
};
console.log(111, options.universal.host);

const callLib = new CallApp(options);

/**
 * 获取 browser 信息
 */
function getBrowser() {
  let ua = window.navigator.userAgent || "";
  console.log(ua);
  let isAndroid = /android/i.test(ua);
  let isIos = /iphone|ipad|ipod/i.test(ua);
  let isWechat = /micromessenger\/([\d.]+)/i.test(ua);
  let isWeibo = /(weibo).*weibo__([\d.]+)/i.test(ua);
  let isQQ = /qq\/([\d.]+)/i.test(ua);
  let isQQBrowser = /(qqbrowser)\/([\d.]+)/i.test(ua);
  let isQzone = /qzone\/.*_qz_([\d.]+)/i.test(ua);
  // 安卓 chrome 浏览器，很多 app 都是在 chrome 的 ua 上进行扩展的
  let isOriginalChrome =
    /chrome\/[\d.]+ Mobile Safari\/[\d.]+/i.test(ua) && isAndroid;
  // chrome for ios 和 safari 的区别仅仅是将 Version/<VersionNum> 替换成了 CriOS/<ChromeRevision>
  // ios 上很多 app 都包含 safari 标识，但它们都是以自己的 app 标识开头，而不是 Mozilla
  let isSafari =
    /safari\/([\d.]+)$/i.test(ua) &&
    isIos &&
    ua.indexOf("Crios") < 0 &&
    ua.indexOf("Mozilla") === 0 &&
    !/chrome/i.test(ua) &&
    !/browser/i.test(ua);

  //IOS端 iphone7及以上UC浏览器无iphone标识，变为windows
  let isIosNewUCBrowser = /windows/i.test(ua) && /ucbrowser/i.test(ua);
  let isBaiduApp = /baidubox/i.test(ua);
  let isSouGou = /sougou/i.test(ua) || /sogou/i.test(ua);
  let is360 = /qhbrowser/i.test(ua) || /360/i.test(ua);
  let isOppoBrowser = /OppoBrowser/i.test(ua);
  let isXiaoMiBrowser = /MiuiBrowser/i.test(ua);
  return {
    isAndroid: isAndroid,
    isIos: isIos,
    isWechat: isWechat,
    isWeibo: isWeibo,
    isQQ: isQQ,
    isQQBrowser: isQQBrowser,
    isQzone: isQzone,
    isOriginalChrome: isOriginalChrome,
    isSafari: isSafari,
    isIosNewUCBrowser: isIosNewUCBrowser,
    isBaiduApp: isBaiduApp,
    isSouGou: isSouGou,
    is360: is360,
    isOppoBrowser: isOppoBrowser,
    isXiaoMiBrowser: isXiaoMiBrowser,
  };
}

/**
 * 获取 ios 大版本号
 */
function getIOSVersion() {
  let result = <any>null;
  let version = navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/);
  if (version && version.length > 1) {
    result = parseInt(version[1], 10);
  } else {
    result = version;
  }
  return result;
}

/**
 * 获取 微信 版本号
 */
function getWeChatVersion() {
  let version = navigator.appVersion.match(/micromessenger\/(\d+\.\d+\.\d+)/i);
  return version && version.length > 0 ? version[1] : "";
}

function guideMethod(textDes: any) {
  let text = "点击右上角··· -> 在默认中打开";
  if (!!text) {
    text = textDes;
  }
  let div = document.createElement("div");

  div.style.position = "fixed";
  div.style.top = "0";
  div.style.left = "0";
  div.style.zIndex = "11111111111";
  div.style.width = "100vw";
  div.style.height = "100vh";
  div.style.backgroundColor = "rgba(0,0,0,0.7)";
  div.innerHTML =
    '<img style="position: absolute;top: 2vw;right: 10vw;width: 12vw;height:18vw;" src="https://ohudong.cztv.com/1/260556/images/guideArrow.png"><div style="position: absolute;top: 22vw;right: 20vw;font-size: 4vw;color: #fff;">' +
    text +
    "</div>";

  document.body.appendChild(div);
  div.onclick = function () {
    div.remove();
  };
}

export const callApp = {
  open: (url: string) => {
    let browser = getBrowser();
    if (
      browser.isIos &&
      (browser.isWechat || browser.isQQ || browser.isWeibo)
    ) {
      console.log("isIos");
      guideMethod("点击右上角··· -> 在safari中打开");
    } else if (
      browser.isAndroid &&
      (browser.isWechat || browser.isWeibo || browser.isQQ)
    ) {
      console.log("isAndroidWechat || isAndroidWeibo");
      guideMethod("点击右上角··· -> 在浏览器中打开");
    } else if (
      browser.isAndroid &&
      browser.isOriginalChrome &&
      browser.isBaiduApp
    ) {
      alert("请使用其他浏览器打开");
    } else {
      const urlPath: string =
        window.location.host == "zmtv.cztv.com"
          ? "download/index.html"
          : "ap/download/index.shtml";
      callLib.open({
        param: {
          route: encodeURIComponent(String(url) || ""),
        },
        path: urlPath,
      });
    }
  },
};
